package com.richman.view;



import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;

import com.richman.controller.AutoBetCentralManager;
import com.richman.controller.BetClient;
import com.richman.ps38.entity.BetAdyData;
import com.richman.ps38.entity.BetDvalue;
import com.richman.ps38.entity.Event;
import com.richman.ps38.entity.HGEvent;
import com.richman.ps38.entity.HGPS38Event;
import com.richman.ps38.entity.SendData;
import com.richman.utils.Common;
import com.richman.utils.SoundThread;

import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class BetCentralPane extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -10000L;
	
	private  Vector<String[]> detailsData = new Vector<String[]>();
	
	private Vector<SendData> localgamedetails = new Vector<SendData>();
	
	public Vector<Event> gameDetailsVec = new Vector<Event>(); 
	
	
	
	public Vector<SendData> senddatas = null;
	
	
	public BetWindow betwnd = new BetWindow();
	
	public AutoBetCentralManager autobetCtrMng = new AutoBetCentralManager();
	
	MyTableModel tableMode = new MyTableModel();

	
	JTable table = null;
	
	
	public long lastAddtime = System.currentTimeMillis();

	/**
	 * Create the panel.
	 */
	public BetCentralPane() {
		setLayout(new BorderLayout(0, 0));
		

		
		JPanel funBtnPane = new JPanel();
		
		JButton cleanButton = new JButton("清空数据");
		cleanButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				int n = JOptionPane.showConfirmDialog(null, "确认要清空数据吗?", "清空数据",
						JOptionPane.YES_NO_OPTION);
				if (n == JOptionPane.YES_OPTION) {					
					if(detailsData != null && detailsData.size() != 0) {
						detailsData.clear();
						localgamedetails.clear();
						tableMode.updateTable();
					}
				}
				
			}
		});
		
		cleanButton.setBounds(20, 10, 60, 20);
		
		funBtnPane.add(cleanButton);
		
		add(funBtnPane, BorderLayout.NORTH);
		
	    table = new JTable(tableMode);
	    
	    table.getColumnModel().getColumn(0).setPreferredWidth(120);
	    
	    table.setRowHeight(30);
	    
	    table.setFont(new java.awt.Font("黑体", Font.PLAIN, 15));
		
		
		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane, BorderLayout.CENTER);
		

		table.addKeyListener(new KeyAdapter() {

			@Override

			public void keyPressed(KeyEvent e) {

			super.keyPressed(e);

			char c = e.getKeyChar();

			int code = e.getKeyCode();
			
			String text  = "";
			
			
				
				
				if(code == 67) {
					
					text  = "focus on JTable 你按下的键的信息：keyChar = " + c + " KeyCode = " + code;
					
					showBetWnd();
					
					System.out.println(text);
					
				}
				
				
				
			

			

			

			}

			});
        
	
        
		
		//双击table事件
	    table.addMouseListener(new MouseAdapter(){

            public void mouseClicked(MouseEvent e) {
            	
            	
            	
          	  if(e.getClickCount() == 2){
          		  
          		  
          		  boolean bfind = false;
          		  
//          		  if(DaShuiClient.mainWnd.getautobet() == true) {
//          			JOptionPane.showMessageDialog(null,"如需手动投注,请先取消自动投注");
//          			return;
//          		  }
          		  
          		  if(!(BetClient.betCtrMng.hgmanager.getStatuscode() == 1)){
          			JOptionPane.showMessageDialog(null,"皇冠账号尚未完成登录，请稍后投注");
          			return;
          		  }
          		  
          		  
          		  if(!(BetClient.betCtrMng.ps38manager.getStatuscode() == 2)){
          			JOptionPane.showMessageDialog(null,"ps38账号尚未完成登录，请稍后投注");
          			return;
          		  }
          		  
          		  
              	  int row =((JTable)e.getSource()).rowAtPoint(e.getPoint()); //获得行位置 
              	  
              	if(betwnd.isVisible()) {
    				return;
    			}
              	  
              	  
              	  if(detailsData.elementAt(row)[0].equals("")){
              		  return;
              	  }else{
              		  
//              		  String eventid = detailsData.elementAt(row)[10];
//              		  
//              		  double pankou = Double.parseDouble(detailsData.elementAt(row)[6]);
              		  
              		  
              		  
        				if(betwnd.setgame(detailsData.elementAt(row)[5], detailsData.elementAt(row)[11], localgamedetails.elementAt(row))) {
        					betwnd.setVisible(true);
        				}  
        				
        				
//        				bfind = true;
//        				break;
              		  
              		  
              		  
//              		  for(int i = 0; i < senddatas.size(); i++){
//              			  
//              			  String bettype = detailsData.elementAt(row)[5];
//              			  
//              			  String pankoustr = "";
//              			  String ps38pankoustr = "";
//              			  
//              			  if(bettype.contains("全场让球")) {
//              				  
//              				pankoustr = senddatas.elementAt(i).hgevent.wholerqppankou;
//              				ps38pankoustr = senddatas.elementAt(i).event.wholerqppankou;
//              				  
//              			  }else if(bettype.contains("全场大小")) {
//              				pankoustr = senddatas.elementAt(i).hgevent.wholeDXPPankou;
//              				ps38pankoustr = senddatas.elementAt(i).event.wholeDXPPankou;
//              			  }
//              			  
//              			  
//              			  
//              			if(!Common.isNum(pankoustr)) {
//              				
//              				
//              				//Common.logOut("手动投注  pankoustr:" + pankoustr);
//              				
//              				continue;
//              			  }
//              			  
//              			  
//              			  
//              			  if(eventid.equals(senddatas.elementAt(i).hgevent.eventId) && 
//              					  Math.abs(pankou - Double.parseDouble(pankoustr)) < 0.001 &&
//              					Math.abs(pankou - Double.parseDouble(ps38pankoustr)) < 0.001 ) {
//              				  
//              				betwnd.setgame(detailsData.elementAt(row)[5], detailsData.elementAt(row)[11], senddatas.elementAt(i));  
//              				betwnd.setVisible(true);
//              				
//              				bfind = true;
//              				break;
//              			  }
//              			  
//              			  
//              		  }
              		  
              		  
              		  //betwnd.setgame(gameDetailsVec.elementAt(0));
    				  
              		  
              		  
              	  }
              	  
              	  
//              	  if(bfind == false) {
//              		JOptionPane.showMessageDialog(null,"该记录已失效");
//              	  }
              	  
              	  
              	  
              	  
              	  
          	  }else{
          		  return;
          	  }

            }
      });
		

	}
	
	
	
	public void updatePS38EventTable(Vector<SendData> gamedetails){
		
		try{
			
			if(!(BetClient.betCtrMng.hgmanager.getStatuscode() == 1) || !(BetClient.betCtrMng.ps38manager.getStatuscode() == 2) ){
      			
      			return;
      		  }
      		  

			boolean  autobet = BetClient.mainWnd.getautobet();
			
			if(autobet == false && betwnd.isShowing() == true) {
				return;
			}
			
			
			if(BetClient.hasIllegalBet == true && BetClient.mainWnd.getIgnoreWarn() == false) {
				BetClient.mainWnd.setWraningBackGround();
				
				if(SoundThread.inPlay == false){    			
					SoundThread st = new SoundThread();
					SoundThread.status = 4;
					st.start();
				}
				
				return;
				
			}
			
			
			SimpleDateFormat dfDay = new SimpleDateFormat("yyyy-MM-dd");
			int forbidst = BetClient.mainWnd.getforbidst();
			int forbided = BetClient.mainWnd.getforbided();
			
			String day = dfDay.format(System.currentTimeMillis());
			
			long dayst = dfDay.parse(day).getTime();
			
			long currentT = System.currentTimeMillis();
			
			if(forbided >= forbidst) {
				if(currentT >= dayst + forbidst*60*60*1000 && currentT <= dayst + forbided*60*60*1000) {
					return;
				}
			}else {
				if(currentT >= dayst + forbidst*60*60*1000 && currentT <= dayst + 24*60*60*1000 + forbided*60*60*1000) {
					return;
				}
			}
			
			
			
			boolean bbetdanshi = BetClient.mainWnd.getbetdanshi();
			boolean bbetrunning = BetClient.mainWnd.getbetrunning();
			
			
			Vector<BetAdyData> bads = BetClient.betCtrMng.getBetAdyData();
			
			
			Map<String, Integer> betCounts = new HashMap<String, Integer>(); 

			int betCountLimit = BetClient.mainWnd.getbetlimit();
			
			
			
			for(BetAdyData bad : bads) {
				
				String typetmp = bad.bettype;
				
				if( bad.hgbetres.equals("0") || bad.hgbetres.equals("1") || bad.hgbetres.equals("2") ) {
					
//					if(typetmp.contains("大小")) {
//						typetmp = "大小";
//					}else {
//						typetmp = "让球";
//					}
					
					String key = bad.hgeventid + typetmp;
					
					Integer count = betCounts.get(key);
					betCounts.put(key, count == null?1:count+1);
					
				}
				

				
			}
			
			
			
			int revDataItl = BetClient.mainWnd.getReceiveDataInterval();
			
			int notsamematchRecDataItl = BetClient.mainWnd.getnotsamematchReceiveDataInterval();
			
			boolean bfilter = BetClient.mainWnd.getMatchFilter();
			
			senddatas = gamedetails;
			
			boolean bethalf = BetClient.mainWnd.getBetHalf();
			boolean betwhole = BetClient.mainWnd.getBetWhole();
			
			
			double betodd = 0.0;
			
			
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			SimpleDateFormat dfSec = new SimpleDateFormat("HH:mm:ss");
			
			String currentTimestr = dfSec.format(System.currentTimeMillis());
			
			for(int i = 0; i < gamedetails.size(); i++) {
				
				Event game = gamedetails.elementAt(i).event;
				
				HGEvent hggame = gamedetails.elementAt(i).hgevent;
				
				HGPS38Event hgps38game = gamedetails.elementAt(i).hgps38event;
				
				String typestr = "";
				
				
				String time = hggame.duringTime;
				if(time.equals("")) {
					time = hggame.eventTime;
					typestr = "单式";
					
					if(!bbetdanshi) {
						continue;
					}
					
				}else {

					typestr = "滚球";
					
					if(!bbetrunning) {
						continue;
					}

					
				}

				
				String hgps38 = "1";
				String ps38hg = "2";
				
				
				
				
				boolean betpankou = BetClient.mainWnd.getbetpankou();
				boolean betdxq = BetClient.mainWnd.getbetdxq();
				
				
				
				//全场让球
				if( (!hgps38game.wholerqppankou.equals("")) && betwhole && betpankou) {
					
					String typestr1 = typestr + "全场让球";
					
					double hgodd = 0.0;
					double ps38odd = 0.0;
					
					if(!hggame.wholeRQPPankouUp.equals("") && !game.wholeRQPPankouDown.equals("")) {
						
						hgodd = Double.parseDouble(hggame.wholeRQPPankouUp);
						ps38odd = Double.parseDouble(game.wholeRQPPankouDown);
					
					}
					
					double hgodd1 = 0.0;
					double ps38odd1 = 0.0;
					
					if(!hggame.wholeRQPPankouDown.equals("") && !game.wholeRQPPankouUp.equals("")) {
						
						hgodd1 = Double.parseDouble(hggame.wholeRQPPankouDown);
						ps38odd1 = Double.parseDouble(game.wholeRQPPankouUp);
					}
					
					
					if(!hgps38game.wholeRQPPankouUp.equals("") && Double.parseDouble(hgps38game.wholeRQPPankouUp) >= betodd && isInRange(hgodd, ps38odd)) {
						
						
						
						
						
						
						
						
						
						
						
						String[] item = {Integer.toString(detailsData.size()+1),
								currentTimestr,
								hggame.leagueName,
								time, 
								hggame.homeTeam + "-vs-" + hggame.awayTeam,
								typestr1, 
								hggame.wholerqppankou, 
								hggame.homeTeam + "@" + Common.showRatio(hggame.wholeRQPPankouUp),
								hggame.awayTeam + "@" + game.wholeRQPPankouDown,
								hgps38game.wholeRQPPankouUp,
								hggame.eventId,//10
								hgps38, //11
								Long.toString(System.currentTimeMillis()),//12
								"0"//13
								};
						
						boolean filterDuitou = false;
						
						//增加过滤对头单以进注单为准
						if(bfilter == true) {
							for(BetAdyData betdata: bads) {
								if(betdata.hgeventid.equals(hggame.eventId) && betdata.bettype.contains("全场让球") && !betdata.hgps38.equals(hgps38)) {
									filterDuitou = true;
									break;
								}
							}
						}
						
						
						
						boolean isReachTime = true;

						
						for(String[] oldItem: detailsData) {
							
							if(oldItem[5].equals(typestr1) && oldItem[10].equals(hggame.eventId) && oldItem[6].equals(item[6]) && oldItem[7].equals(item[7])
									&&  oldItem[8].equals(item[8]) && oldItem[9].equals(item[9])  && oldItem[11].equals(hgps38) ) {
								
								
									
									if(System.currentTimeMillis() - Long.parseLong(oldItem[12]) < revDataItl*1000) {
										isReachTime = false;
										break;
									}
								
						
							}

							
							//删除根据投注中心过滤对头单
//							if(oldItem[5].contains("全场让球") && oldItem[10].equals(hggame.eventId) && !oldItem[11].equals(hgps38)) {
//								if(bfilter == true) {//过滤对头单
//									isReachTime = false;
//									break;
//								}
//							}
							
						}
						
						
						
						
						
						if(isReachTime == true && filterDuitou == false) {
							
							
							
							boolean skip = false;
							for(BetAdyData betdata: bads) {
								if(betdata.hgeventid.equals(hggame.eventId) && betdata.bettype.equals(typestr1) && Math.abs(Double.parseDouble(betdata.betpankou) - Double.parseDouble(item[6])) < 0.01 && System.currentTimeMillis() - Long.parseLong(betdata.hgbettime) < revDataItl*1000) {
									skip =true;
									break;
								}
							}
							
							if(autobet == false && (System.currentTimeMillis() - lastAddtime < notsamematchRecDataItl*1000 || System.currentTimeMillis() - lastAddtime < revDataItl*1000 )) {
								skip = true;
							}
							

							if(skip == false && System.currentTimeMillis() - BetClient.betCtrMng.lastbetsuccesstime > notsamematchRecDataItl*1000 && 
									(betCounts.get(hggame.eventId + typestr1) == null || betCounts.get(hggame.eventId + typestr1) < betCountLimit)) {
								
								
								
								detailsData.add(0, item);
								localgamedetails.add(0,gamedetails.elementAt(i));
								lastAddtime = System.currentTimeMillis();
								
								//发出声音
					    		if(SoundThread.inPlay == false){    			
									SoundThread st = new SoundThread();
									SoundThread.status = 3;
									st.start();
								}
					    		
					    		break;
								
							}

						}
						
						
						
						
					}

					if(!hgps38game.wholeRQPPankouDown.equals("") && Double.parseDouble(hgps38game.wholeRQPPankouDown) >= betodd&& isInRange(hgodd1, ps38odd1)) {
						
					
						
						String[] item = {Integer.toString(detailsData.size()+1),currentTimestr,hggame.leagueName, time, hggame.homeTeam + "-vs-" + hggame.awayTeam,
								typestr1, hggame.wholerqppankou, hggame.awayTeam + "@" + Common.showRatio(hggame.wholeRQPPankouDown), hggame.homeTeam + "@" + game.wholeRQPPankouUp, 
								hgps38game.wholeRQPPankouDown, hggame.eventId, ps38hg, Long.toString(System.currentTimeMillis()), "0"};
						boolean filterDuitou = false;
						
						//增加过滤对头单以进注单为准
						if(bfilter == true) {
							for(BetAdyData betdata: bads) {
								if(betdata.hgeventid.equals(hggame.eventId) && betdata.bettype.contains("全场让球") && !betdata.hgps38.equals(ps38hg)) {
									filterDuitou = true;
									break;
								}
							}
						}
						
						
						boolean isReachTime = true;
						
						for(String[] oldItem: detailsData) {
							
							if(oldItem[5].equals(typestr1) && oldItem[10].equals(hggame.eventId) && oldItem[6].equals(item[6]) && oldItem[7].equals(item[7])
									&&  oldItem[8].equals(item[8]) && oldItem[9].equals(item[9])  && oldItem[11].equals(ps38hg) ) {
								
								
									
									if(System.currentTimeMillis() - Long.parseLong(oldItem[12]) < revDataItl*1000) {
										isReachTime = false;
										break;
									}
								
						
							}

							
//							if(oldItem[5].contains("全场让球") && oldItem[10].equals(hggame.eventId) && !oldItem[11].equals(ps38hg)) {
//								if(bfilter == true) {//过滤对头单
//									isReachTime = false;
//									break;
//								}
//							}
							
						}
						
						
						
						if(isReachTime == true &&filterDuitou == false) {
							
							boolean skip = false;
							for(BetAdyData betdata: bads) {
								if(betdata.hgeventid.equals(hggame.eventId) && 
										betdata.bettype.equals(typestr1) && 
										Math.abs(Double.parseDouble(betdata.betpankou) - Double.parseDouble(item[6])) < 0.01 && 
										System.currentTimeMillis() - Long.parseLong(betdata.hgbettime) < revDataItl*1000) {
									skip =true;
									break;
								}
							}
							
							if(autobet == false && (System.currentTimeMillis() - lastAddtime < notsamematchRecDataItl*1000 || System.currentTimeMillis() - lastAddtime < revDataItl*1000 )) {
								skip = true;
							}
							

							if(skip == false && System.currentTimeMillis() - BetClient.betCtrMng.lastbetsuccesstime > notsamematchRecDataItl*1000 && 
									(betCounts.get(hggame.eventId + typestr1) == null || betCounts.get(hggame.eventId + typestr1) < betCountLimit)) {
								detailsData.add(0, item);
								localgamedetails.add(0,gamedetails.elementAt(i));
								lastAddtime = System.currentTimeMillis();
								
								//发出声音
					    		if(SoundThread.inPlay == false){    			
									SoundThread st = new SoundThread();
									SoundThread.status = 3;
									st.start();
								}
					    		
					    		break;
								
							}

						}
						
					}

				}
				
				

				
				
				//全场大小
				if((!hgps38game.wholeDXPPankou.equals("")) && betwhole && betdxq) {
					
					String typestr1 = typestr + "全场大小";

					double hgodd = 0.0;
					double ps38odd = 0.0;
					
					if(!hggame.wholeDXPPankouUp.equals("") && !game.wholeDXPPankouDown.equals("")) {
						
						hgodd = Double.parseDouble(hggame.wholeDXPPankouUp);
						ps38odd = Double.parseDouble(game.wholeDXPPankouDown);
					}
					
					double hgodd1 = 0.0;
					double ps38odd1 = 0.0;
					
					if(!hggame.wholeDXPPankouDown.equals("") && !game.wholeDXPPankouUp.equals("")) {
						hgodd1 = Double.parseDouble(hggame.wholeDXPPankouDown);
						ps38odd1 = Double.parseDouble(game.wholeDXPPankouUp);
					}
					
					
						if(!hgps38game.wholeDXPPankouUp.equals("") && Double.parseDouble(hgps38game.wholeDXPPankouUp) >= betodd && isInRange(hgodd, ps38odd)) {
							
							
								String[] item = {Integer.toString(detailsData.size()+1),
										currentTimestr,
										hggame.leagueName, 
										time, 
										hggame.homeTeam + "-vs-" + hggame.awayTeam,
										typestr1, hggame.wholeDXPPankou,  
										"大@" + Common.showRatio(hggame.wholeDXPPankouUp),  
										"小@" + game.wholeDXPPankouDown, 
										hgps38game.wholeDXPPankouUp,
										hggame.eventId, 
										hgps38,
										Long.toString(System.currentTimeMillis()), "0"};
								
								boolean filterDuitou = false;
								
								//增加过滤对头单以进注单为准
								if(bfilter == true) {
									for(BetAdyData betdata: bads) {
										if(betdata.hgeventid.equals(hggame.eventId) && betdata.bettype.contains("全场大小") && !betdata.hgps38.equals(hgps38)) {
											filterDuitou = true;
											break;
										}
									}
								}
								
								boolean isReachTime = true;
								
								for(String[] oldItem: detailsData) {
									
									if(oldItem[5].equals(typestr1) && oldItem[10].equals(hggame.eventId) && oldItem[6].equals(item[6]) && oldItem[7].equals(item[7])
											&&  oldItem[8].equals(item[8]) && oldItem[9].equals(item[9])  && oldItem[11].equals(hgps38) ) {
										
										
											
											if(System.currentTimeMillis() - Long.parseLong(oldItem[12]) < revDataItl*1000) {
												isReachTime = false;
												break;
											}
										
								
									}
									
									
//									if(oldItem[5].contains("全场大小") && oldItem[10].equals(hggame.eventId) && !oldItem[11].equals(hgps38)) {
//										if(bfilter == true) {//过滤对头单
//											isReachTime = false;
//											break;
//										}
//									}
									
									
									
								}
								
								
								
								if(isReachTime == true && filterDuitou == false) {
									
									boolean skip = false;
									for(BetAdyData betdata: bads) {
										if(betdata.hgeventid.equals(hggame.eventId) && betdata.bettype.equals(typestr1)&&Math.abs(Double.parseDouble(betdata.betpankou) - Double.parseDouble(item[6])) < 0.01 && System.currentTimeMillis() - Long.parseLong(betdata.hgbettime) < revDataItl*1000) {
											skip =true;
											break;
										}
									}
									
									
									if(autobet == false && (System.currentTimeMillis() - lastAddtime < notsamematchRecDataItl*1000 || System.currentTimeMillis() - lastAddtime < revDataItl*1000 )) {
										skip = true;
									}
									
									if(skip == false && System.currentTimeMillis() - BetClient.betCtrMng.lastbetsuccesstime > notsamematchRecDataItl*1000&& 
											(betCounts.get(hggame.eventId + typestr1) == null || betCounts.get(hggame.eventId + typestr1) < betCountLimit)) {
										detailsData.add(0, item);
										localgamedetails.add(0,gamedetails.elementAt(i));
										lastAddtime = System.currentTimeMillis();
										
										//发出声音
							    		if(SoundThread.inPlay == false){    			
											SoundThread st = new SoundThread();
											SoundThread.status = 3;
											st.start();
										}
							    		break;
										
									}

								}
							
						}

						if(!hgps38game.wholeDXPPankouDown.equals("") && Double.parseDouble(hgps38game.wholeDXPPankouDown) >= betodd && isInRange(hgodd1, ps38odd1)) {
							
								String[] item = {Integer.toString(detailsData.size()+1),currentTimestr,hggame.leagueName, time, hggame.homeTeam + "-vs-" + hggame.awayTeam,
										typestr1, hggame.wholeDXPPankou, "小@" + Common.showRatio(hggame.wholeDXPPankouDown), "大@" + game.wholeDXPPankouUp, 
										hgps38game.wholeDXPPankouDown,hggame.eventId, ps38hg,Long.toString(System.currentTimeMillis()), "0"};

								boolean filterDuitou = false;
								
								//增加过滤对头单以进注单为准
								if(bfilter == true) {
									for(BetAdyData betdata: bads) {
										if(betdata.hgeventid.equals(hggame.eventId) && betdata.bettype.contains("全场大小") && !betdata.hgps38.equals(ps38hg)) {
											filterDuitou = true;
											break;
										}
									}
								}
								
								
								boolean isReachTime = true;
								
								for(String[] oldItem: detailsData) {
									
									
									if(oldItem[5].equals(typestr1) && oldItem[10].equals(hggame.eventId) && oldItem[6].equals(item[6]) && oldItem[7].equals(item[7])
											&&  oldItem[8].equals(item[8]) && oldItem[9].equals(item[9])  && oldItem[11].equals(ps38hg) ) {
										
										
											
											if(System.currentTimeMillis() - Long.parseLong(oldItem[12]) < revDataItl*1000) {
												isReachTime = false;
												break;
											}
										
								
									}
									
									if(oldItem[5].contains("全场大小") && oldItem[10].equals(hggame.eventId) && !oldItem[11].equals(ps38hg)) {
										if(bfilter == true) {//过滤对头单
											isReachTime = false;
											break;
										}
									}
									
								}
								
								
								
								if(isReachTime == true && filterDuitou == false) {
									
									boolean skip = false;
									for(BetAdyData betdata: bads) {
										if(betdata.hgeventid.equals(hggame.eventId) && betdata.bettype.equals(typestr1) &&Math.abs(Double.parseDouble(betdata.betpankou) - Double.parseDouble(item[6])) < 0.01&& System.currentTimeMillis() - Long.parseLong(betdata.hgbettime) < revDataItl*1000) {
											skip =true;
											break;
										}
									}
									
									
									if(autobet == false && (System.currentTimeMillis() - lastAddtime < notsamematchRecDataItl*1000 || System.currentTimeMillis() - lastAddtime < revDataItl*1000 )) {
										skip = true;
									}
									
									
									if(skip == false && System.currentTimeMillis() - BetClient.betCtrMng.lastbetsuccesstime > notsamematchRecDataItl*1000&& 
											(betCounts.get(hggame.eventId + typestr1) == null || betCounts.get(hggame.eventId + typestr1) < betCountLimit)) {
										detailsData.add(0,item);
										localgamedetails.add(0,gamedetails.elementAt(i));
										lastAddtime = System.currentTimeMillis();
										
										//发出声音
							    		if(SoundThread.inPlay == false){
											SoundThread st = new SoundThread();
											SoundThread.status = 3;
											st.start();
										}
							    		break;
										
									}

								}
								
							
						}

				}

				

				
				
			}
			
			
			
			
			
			
			
			
			setOneRowBackgroundColor();
			
			tableMode.updateTable();
			
			int delaybettime = BetClient.mainWnd.getdelaybettime();
			
			if(delaybettime != 0) {
				Thread.sleep(delaybettime*1000);
			}
			
			
			
			
			//constructBetDvalue();
			
			
			
			processAutoBet();
			

			
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	
	
//	private void constructBetDvalue() {
//		
//		
//		try {
//			
//			Vector<String> parsealready = new Vector<String>();
//			
//			
//			if(DaShuiClient.betCtrMng.betDvalues != null) {
//				DaShuiClient.betCtrMng.betDvalues.clear();
//			}
//			
//			
//			double maxdvalue = DaShuiClient.mainWnd.getmaxdvalue();
//			
//			for(int i = 0; i < detailsData.size(); i++) {
//				
//				String[] item = detailsData.elementAt(i);
//				
//				
//				if(parsealready.contains(item[10] + item[5].substring(2))) {
//					continue;
//				}
//				
//				
//				parsealready.add(item[10] + item[5].substring(2));
//				
//				
//				boolean bfirstpankou = true;
//				
//				String sfirstpankou = "";
//				
//				for(int j = detailsData.size()-1; j >=0; j--) {
//					
//					
//					String[] item1 = detailsData.elementAt(j);
//					
//					if(item1[10].equals(item[10]) && 
//					   item1[5].substring(2).equals(item[5].substring(2))) {
//						
//						if(bfirstpankou == true) {						
//							
//							bfirstpankou = false;
//							sfirstpankou = item1[6];
//							
//							BetDvalue bdvalue = new BetDvalue();
//							bdvalue.firstpankou = sfirstpankou;
//							bdvalue.league = item1[2];
//							bdvalue.team = item1[4];
//							bdvalue.bettype = item1[5];
//							bdvalue.eventid = item1[10];
//							if(item1[11].equals("1")) {
//								bdvalue.hgps38times++;
//							}else if(item1[11].equals("2")) {
//								bdvalue.ps38hgtimes++;
//							}
//							
//							DaShuiClient.betCtrMng.betDvalues.add(bdvalue);
//							
//
//						}else {
//							
//							for(int k = 0; k < DaShuiClient.betCtrMng.betDvalues.size(); k++) {
//								
//								if(DaShuiClient.betCtrMng.betDvalues.elementAt(k).eventid.equals(item1[10]) &&
//								   DaShuiClient.betCtrMng.betDvalues.elementAt(k).bettype.substring(2).equals(item1[5].substring(2)) &&
//								   Math.abs(Double.parseDouble(DaShuiClient.betCtrMng.betDvalues.elementAt(k).firstpankou) - Double.parseDouble(item1[6])) <= maxdvalue) {
//									
//									if(item1[11].equals("1")) {
//										DaShuiClient.betCtrMng.betDvalues.elementAt(k).hgps38times++;
//									}else if(item1[11].equals("2")) {
//										DaShuiClient.betCtrMng.betDvalues.elementAt(k).ps38hgtimes++;
//									}
//									
//									
//									break;
//									
//								}
//								
//							}
//							
//						}
//						
//					}
//					
//				}
//				
//				
//			}
//			
//			
////			for(int i = 0; i < DaShuiClient.betCtrMng.betDvalues.size(); i++) {
////				System.out.println(DaShuiClient.betCtrMng.betDvalues.elementAt(i).toString());
////			}
//			
//			
//			
//			DaShuiClient.mainWnd.updateBetDvalueTable(DaShuiClient.betCtrMng.betDvalues);
//			
//			
//		}catch(Exception e) {
//			e.printStackTrace();
//		}
//		
//		
//
//	}
//


	public void processAutoBet() {
		
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		
		
		
		if(BetClient.mainWnd.getautobet() == true) {
			
			for(int i = 0; i < detailsData.size(); i++) {
				
				if(senddatas != null){
            		  
            		  String eventid = detailsData.elementAt(i)[10];
            		  
            		  double pankou = Double.parseDouble(detailsData.elementAt(i)[6]);
            		  
            		  for(int j = 0; j < senddatas.size(); j++){
            			  
            			  
            			  String bettype = detailsData.elementAt(i)[5];
              			  
              			  String pankoustr = "";
              			  String ps38pankoustr = "";
              			  
              			  if(bettype.contains("全场让球")) {
              				  
              				pankoustr = senddatas.elementAt(j).hgevent.wholerqppankou;
              				ps38pankoustr = senddatas.elementAt(j).event.wholerqppankou;
              				  
              			  }else if(bettype.contains("全场大小")) {
              				pankoustr = senddatas.elementAt(j).hgevent.wholeDXPPankou;
              				ps38pankoustr = senddatas.elementAt(j).event.wholeDXPPankou;
              			  }
              			  
              			  
              			  
              			  
              			  
              			  if(!Common.isNum(pankoustr) || !Common.isNum(ps38pankoustr)) {
//              				System.out.println(df.format(System.currentTimeMillis()) + "  processAutoBet  pankoustr is not a number:" + Arrays.toString(detailsData.elementAt(i)));
//              				
//              				Common.logOut("pankoustr:" + pankoustr);
              				
              				continue;
              			  }
            			  
            			  
            			  Long timepassed = System.currentTimeMillis() - Long.parseLong(detailsData.elementAt(i)[12]);
            			  
            			  if(Double.parseDouble(detailsData.elementAt(i)[9]) >= BetClient.mainWnd.getbetodd() && 
            					  eventid.equals(senddatas.elementAt(j).hgevent.eventId) && 
            					  Math.abs(pankou - Double.parseDouble(pankoustr))  < 0.001&&
            					  Math.abs(pankou - Double.parseDouble(ps38pankoustr)) < 0.001 &&
            					  detailsData.elementAt(i)[13].equals("0") && 
            					  timepassed < BetClient.mainWnd.getReceiveDataInterval()*1000) {
            				
            				System.out.println(df.format(System.currentTimeMillis()) + "  processAutoBet start to bet:  " + Arrays.toString(detailsData.elementAt(i)));
            				
            				BetClient.isInbet = true;
            				
            				autobetCtrMng.dobetgame(detailsData.elementAt(i)[5], detailsData.elementAt(i)[11], senddatas.elementAt(j));
            				
            				detailsData.elementAt(i)[13] = "1";
            				
            			  }
            			  
            			  
            		  }

            	  }
				
			}
			
			
			
			BetClient.isInbet = false;
			
			
		}
		
	}
	
	
    public void setOneRowBackgroundColor() {

        try {  
        	
            DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
            	
            	
  
                public Component getTableCellRendererComponent(JTable table,  
                        Object value, boolean isSelected, boolean hasFocus,  
                        int row, int column) {  

                	
                    if (row%2==1) {  
                    	setBackground(new Color(246,246,246));  
                        
                    }else{  
                        setBackground(new Color(222,222,243));  
                    	//setBackground(new Color(1,1,1));  
                      
                    }  
                    
                    setHorizontalAlignment(JLabel.CENTER);
  
                    return super.getTableCellRendererComponent(table, value,  
                            isSelected, hasFocus, row, column);  
                }  
            };  
            int columnCount = table.getColumnCount();  
            for (int i = 0; i < columnCount; i++) {  
            	table.getColumn(table.getColumnName(i)).setCellRenderer(tcr);
            }  
        } catch (Exception ex) {  
            ex.printStackTrace();  
        }  
    
    }  
	
	
	
	

    public class MyTableModel extends AbstractTableModel  
    {  
        /**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		/* 
         * 这里和刚才一样，定义列名和每个数据的值 
         */  
        String[] columnNames =  
        { "序号", "时间",  "联赛", "比赛时间", "球队", "类型", "盘口", "皇冠", "PS38", "合并赔率"};
        
       // { "序号", "联赛", "时间", "球队", "类型", "盘口", "皇冠", "PS38", "合并赔率","1","2","时间"};
        

  
        /** 
         * 构造方法，初始化二维数组data对应的数据 
         */  
        public MyTableModel()  
        {  

        }  
  
        // 以下为继承自AbstractTableModle的方法，可以自定义  
        /** 
         * 得到列名 
         */  
        @Override  
        public String getColumnName(int column)  
        {  
            return columnNames[column];  
        }  
          
        /** 
         * 重写方法，得到表格列数 
         */  
        @Override  
        public int getColumnCount()  
        {  
            return columnNames.length;  
        }  
  
        /** 
         * 得到表格行数 
         */  
        @Override  
        public int getRowCount()  
        {         
	        if(null == detailsData){
	    		return 0;
	    	}
            return detailsData.size();  
        }  
  
        /** 
         * 得到数据所对应对象 
         */  
        @Override  
        public Object getValueAt(int rowIndex, int columnIndex)  
        {  
            //return data[rowIndex][columnIndex];
        	
        	if(detailsData.size() == 0) {
        		return null;
        	}
        	
        	return detailsData.elementAt(rowIndex)[columnIndex];
        }  
  
        /** 
         * 得到指定列的数据类型 
         */  
        @Override  
        public Class<?> getColumnClass(int columnIndex)  
        {  
        	
        	return String.class;
        	
//        	if(detailsData.size() == 0){
//        		return String.class;
//        	}
//
//            return detailsData.elementAt(0)[columnIndex].getClass();
        	
        }  
  
        /** 
         * 指定设置数据单元是否可编辑.这里设置"姓名","学号"不可编辑 
         */  
        @Override  
        public boolean isCellEditable(int rowIndex, int columnIndex)  
        {  
            return false;
        }  
          
        /** 
         * 如果数据单元为可编辑，则将编辑后的值替换原来的值 
         */  
        @Override  
        public void setValueAt(Object aValue, int rowIndex, int columnIndex)  
        {  
            detailsData.elementAt(rowIndex)[columnIndex] = (String)aValue;  
            /*通知监听器数据单元数据已经改变*/  
            fireTableCellUpdated(rowIndex, columnIndex);  
        }  
        
        public void updateTable(){
        	fireTableDataChanged();
        }
        
  
    }  
	
	
	
	private boolean isInRange(double hgodd, double ps38odd) {
		
		double sideMinodd = BetClient.mainWnd.getsideMinodd();
		double sumMinodd = BetClient.mainWnd.getCtypesumMinodd();
		
		double sideMaxodd = BetClient.mainWnd.getsideMaxodd();
		double sumMaxodd = BetClient.mainWnd.getsumMaxodd();
		
		
		DecimalFormat df = new DecimalFormat("#.000");
		
		if(ps38odd < 0.0) {
			ps38odd = Double.parseDouble(df.format(2.0 + ps38odd));
		}
		

		
		if(hgodd >= sideMinodd && ps38odd >= sideMinodd && (hgodd + ps38odd) >= sumMinodd && hgodd <= sideMaxodd && ps38odd <= sideMaxodd && (hgodd+ps38odd) <= sumMaxodd) {
			return true;
		}
		
		return false;
	}



	public void showBetWnd() {
		
		if(detailsData != null &&detailsData.size()  != 0) {
			
			if(betwnd.isVisible() || BetClient.mainWnd.getautobet() == true) {
				return;
			}
			
			if(betwnd.setgame(detailsData.elementAt(0)[5], detailsData.elementAt(0)[11], localgamedetails.elementAt(0))) {
				betwnd.setVisible(true);
			}  
			
		}
		

		
	}
	
	
	
	
	
}
